import {
	requestUrl,
	uploadUrl,
	uploadFile
} from "./config";
var TOKEN = uni.getStorageSync('TOKEN') //TOKEN
const request = (options) => {
	return new Promise((resolve, reject) => {
		if (options.type) {
			uni.uploadFile({
				url: uploadFile, //上传图片api
				filePath: options.filePath,
				name: options.name || 'file',
				header: {
					"token": uni.getStorageSync("TOKEN")
				},
				success: (res) => {
					let data = JSON.parse(JSON.stringify(res.data))
					if (JSON.parse(data).code == 0) {
						resolve(JSON.parse(data))
						uni.showToast({
							title: "上传成功",
							icon: "none",
						})
					} else {
						uni.showToast({
							title: "上传失败",
							icon: "error",
						})
					}
				},
				fail(reject) {
					uni.showToast({
						title: "上传失败",
						icon: "none",
					})
					console.log(reject, 'reject');
				}
			});
		} else {
			uni.uploadFile({
				url: uploadUrl, //上传图片api
				filePath: options.filePath,
				name: options.name || 'file',
				header: {
					"token": uni.getStorageSync("TOKEN")
				},
				success: (res) => {
					let data = JSON.parse(JSON.stringify(res.data))
					if (JSON.parse(data).code == 0) {
						resolve(JSON.parse(data))
						uni.showToast({
							title: "上传成功",
							icon: "none",
						})
					} else {
						uni.showToast({
							title: "上传失败",
							icon: "error",
						})
					}

				},
				fail(reject) {
					uni.showToast({
						title: "上传失败",
						icon: "none",
					})
					console.log(reject, 'reject');
				}
			});
		}


	})
}








export default request